package com.fosun.health;

import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.EntityListHandler;
import cn.hutool.db.sql.SqlExecutor;
import cn.hutool.json.JSONArray;
import com.lingd600.gcoord.GcoordTool;
import com.lingd600.proj4js.Proj4JSTool;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class Refresh2000 {
    public static void main(String[] args) throws SQLException {
        DataSource groupDb3 = DbUtil.getDs("group_db1");
        Connection connection = groupDb3.getConnection();

//        handleLine(connection);

        handlePoint(connection);
        connection.close();
    }

    private static void handlePoint(Connection connection) throws SQLException {
        List<Entity> query = SqlExecutor.query(connection, "SELECT objectid, coordinate2000,st_astext(shape) as shape from ps_point_coordinate where district_code = '360981' AND del_flag = 0 and (coordinate2000 is null or coordinate2000 = '[0,0]')", new EntityListHandler());
//        System.out.println(query);

        Set<Entity> collect = query.stream().map(q -> {
            String str = q.getStr("shape");
            Long objectid = q.getLong("objectid");
            Entity entity = new Entity();
            entity.put("objectid", objectid);
            String coordinate2000 = str.replace("POINT ( ", "[").replace(")", "]").trim().replace(" ", ",");
            JSONArray objects = new JSONArray(coordinate2000);
            Double x = objects.getDouble(0);
            Double y = objects.getDouble(1);
            double[] doubles = GcoordTool.AMap2WGS(x, y);
            double[] doubles1 = Proj4JSTool.WGS84ToCGCS2000(doubles[0], doubles[1]);
            entity.put("coordinate2000", String.format("[%s,%s]", doubles1[1], doubles1[0]));
            return entity;
        }).collect(Collectors.toSet());
        collect.forEach(c -> {
//            try {
//                int execute = SqlExecutor.execute(connection, "update ps_point_coordinate set coordinate2000 = ? where objectid = ?", c.getStr("coordinate2000"), c.getLong("objectid"));
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            }
            System.out.println("execute");
        });
    }

    private static void handleLine(Connection connection) throws SQLException {
        List<Entity> query = SqlExecutor.query(connection, "SELECT objectid, coordinate2000,st_astext(shape) as shape from ps_line_coordinate where district_code = '320583' AND del_flag = 0 and objectid = 24", new EntityListHandler());
        Set<Entity> collect = query.stream().map(q -> {
            String str = q.getStr("shape");
            Long objectid = q.getLong("objectid");
            Entity entity = new Entity();
            entity.put("objectid", objectid);
            String coordinate2000 = str.replace("LINESTRING ( ", "[").replace(")", "]").trim().replace(" ", ",").replace(",,", ",");
            JSONArray objects = new JSONArray(coordinate2000);
            Double x = objects.getDouble(0);
            Double y = objects.getDouble(1);
            double[] doubles = GcoordTool.AMap2WGS(x, y);
            double[] doubles1 = Proj4JSTool.WGS84ToCGCS2000(doubles[0], doubles[1]);
            Double x1 = objects.getDouble(2);
            Double y1 = objects.getDouble(3);
            double[] doubles2 = GcoordTool.AMap2WGS(x1, y1);
            double[] doubles3 = Proj4JSTool.WGS84ToCGCS2000(doubles2[0], doubles2[1]);
            entity.put("coordinate2000", String.format("[[%s,%s],[%s,%s]]", doubles1[1], doubles1[0], doubles3[1], doubles3[0]));
            return entity;
        }).collect(Collectors.toSet());
        collect.stream().forEach(c -> {
            try {
                int execute = SqlExecutor.execute(connection, "update ps_line_coordinate set coordinate2000 = ? where objectid = ?", c.getStr("coordinate2000"), c.getLong("objectid"));
                System.out.println(execute);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        });
    }
}
